package corrida.stack;

public class StackSortThread implements Runnable// extends Thread 
{
	
	public Stack stack;
	private Stack aux1;
	private Stack aux2;
	
	
	public StackSortThread () {
		System.out.println("construtor");
		stack = new Stack();
		aux1 = new Stack();
		aux2 = new Stack();
		
		stack.push("04");
		stack.push("07");
		stack.push("09");
		stack.push("08");
		stack.push("01");
		stack.push("10");
		stack.push("03");
		stack.push("02");
		stack.push("05");
		stack.push("06");		
	}

	public void run() {
		stackSort();
	}

	public void stackSort() {
		System.out.println("iniciando");
		Stack pusher = aux1;
		Stack popper = aux2;		
		String value = null;
		
		System.out.println("STACK");
		stack.listElements();
		System.out.println("AUX1");
		aux1.listElements();
		System.out.println("AUX2");
		aux2.listElements();
		
		long t1 = System.nanoTime();
		
		while (!stack.isEmpty()) {
			pusher.push(stack.pop());
			System.out.println("removendo");
		}
		
		popper = aux1;
		pusher = aux2;
		
		while (!pusher.isEmpty() || !popper.isEmpty()) {
			
			value = popper.pop();
			
			while (!popper.isEmpty()) {
				String valueAux = popper.pop();
				
				// the value 0 if the argument string is equal to this string;
				// a value less than 0 if this string is lexicographically
				// less than the string argument;
				// and a value greater than 0 if this string is lexicographically
				// greater than the string argument.
				if (value.compareToIgnoreCase(valueAux) > 0)
				{
					pusher.push(valueAux);
				}
				else
				{
					String temp = value;
					value = valueAux;
					valueAux = temp;
					
					pusher.push(valueAux);
				}								
			}
			
			stack.push(value);
			System.out.println("inserindo");
			Stack temp = pusher;
			pusher = popper;
			popper = temp;			
		}
		
		long t2 = System.nanoTime();
		
		System.out.println("Qtd. tempo gasto = " + (t2 - t1));
		
		System.out.println("STACK");
		stack.listElements();
		System.out.println("AUX1");
		aux1.listElements();
		System.out.println("AUX2");
		aux2.listElements();
		System.out.println("terminando");
	}
}